import 'package:dependencies_examples/event_bus/listener.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';

class UserLoggedInEvent {
  final String userName;

  UserLoggedInEvent(this.userName);
}

final EventBus eventBus = EventBus();

class EventSubject extends StatelessWidget {
  const EventSubject({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            ElevatedButton(
              onPressed: () async {
                eventBus.fire(UserLoggedInEvent('James'));

                await Future.delayed(Duration(seconds: 1));
                if (context.mounted) {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => EventListener()),
                  );
                }
              },
              child: Text('Fire Event'),
            ),
          ],
        ),
      ),
    );
  }
}
